package com.hmy.uaa.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmy.uaa.bo.request.RoleRequest;
import com.hmy.uaa.mapper.RoleMapper;
import com.hmy.uaa.po.*;
import com.hmy.uaa.po.Role;
import com.hmy.common.exception.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 角色信息
 */
@Service
public class RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuRelationService roleMenuRelationService;

    /**
     * 新增角色
     *
     * @param roleRequest 请求参数
     */
    public void save(RoleRequest roleRequest) {
        if (illegalName(roleRequest)) {
            throw new ClientException("角色名称已存在");
        }
        Role role = convertBean(roleRequest);
        role.setCreateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }

    /**
     * 修改角色
     *
     * @param roleRequest 请求参数
     */
    public void update(RoleRequest roleRequest) {

        if (illegalName(roleRequest)) {
            throw new ClientException("角色名称已存在");
        }

        Role role = convertBean(roleRequest);
        role.setId(roleRequest.getId());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.updateById(role);
    }

    /**
     * 删除角色
     *
     * @param id 角色id
     */
    public void delete(Long id) {


        //判断该角色是否绑定菜单
        LambdaQueryWrapper<RoleMenuRelation> roleMenuWrapper = Wrappers.<RoleMenuRelation>lambdaQuery()
                .eq(RoleMenuRelation::getRoleId, id);

        RoleMenuRelation roleMenuRelation = new RoleMenuRelation();
        roleMenuRelation.setRoleId(id);
        boolean isExists = roleMenuRelationService.isBindMenu(roleMenuRelation);
        if (isExists) {
            throw new ClientException("删除失败，该角色有绑定菜单");
        }

        //删除角色
        LambdaQueryWrapper<Role> roleWrapper = Wrappers.<Role>lambdaQuery()
                .eq(Role::getId, id);
        roleMapper.delete(roleWrapper);
    }

    /**
     * 查询角色
     *
     * @param page 分页参数
     * @param name 角色名称
     * @return Page<Role> 返回数据
     */
    public Page<Role> queryPage(Page<Role> page, String name) {
        LambdaQueryWrapper<Role> query = Wrappers.<Role>lambdaQuery();

        //0：表示系统管理员
        query.ne(Role::getId, 0);

        if (Objects.nonNull(name)) {
            query.like(Role::getName, name);
        }

        return roleMapper.selectPage(page, query);
    }


    /**
     * 是否非法角色名称
     */
    private boolean illegalName(RoleRequest roleRequest) {
        LambdaQueryWrapper<Role> queryWrapper = Wrappers.<Role>lambdaQuery()
                .eq(Role::getName, roleRequest.getName());

        if (Objects.nonNull(roleRequest.getId())) {
            queryWrapper.ne(Role::getId, roleRequest.getId());
        }
        return roleMapper.selectCount(queryWrapper) > 0;
    }

    /**
     * 转换bean
     *
     * @param roleRequest 请求参数
     * @return Role 返回转换对象
     */
    private Role convertBean(RoleRequest roleRequest) {
        Role role = new Role();

        role.setName(roleRequest.getName());
        role.setDescription(roleRequest.getDescription());
        role.setStatus(roleRequest.getStatus());
        role.setRoleKey(roleRequest.getRoleKey());
        return role;
    }


}
